#include "ourhdr.h"
#include "ext2.h"

static void display_super_block(char* device){
	struct ext2_super_block super;
	int fd;

	if( (fd = open(device, O_RDONLY)) <0){
		fprintf(stderr, "fail to open %s", device);
		exit(1);
	}
	lseek(fd, 1024, SEEK_SET);
	if (read(fd, &super, sizeof(super)) < 0){
		fprintf(stderr, "fail to read");
		exit(1);
	}
	disp(super.s_inodes_count);
	disp(super.s_blocks_count);
	disp(super.s_r_blocks_count);
	disp(super.s_free_blocks_count);
	disp(super.s_free_inodes_count);
	disp(super.s_first_data_block);
	disp(super.s_log_block_size);
	disp(super.s_log_frag_size);
	disp(super.s_blocks_per_group);
	disp(super.s_frags_per_group);
	disp(super.s_inodes_per_group);
	disp(super.s_mtime);
	disp(super.s_wtime);
	disp(super.s_mnt_count);
	disp(super.s_max_mnt_count);
	disp16(super.s_magic);
	disp(super.s_state);
	disp(super.s_errors);
	disp(super.s_min_rev_level);
	disp(super.s_last_check);
	disp(super.s_check_interval);
	disp(super.s_creator_os);
	disp(super.s_rev_level);
	disp(super.s_def_resuid);
	disp(super.s_def_resgid);
	disp(super.s_first_inode);
	disp(super.s_inode_size);
	disp(super.s_block_group_nr);
	disp(super.s_feature_compat);
	disp(super.s_feature_incompat);
	disp(super.s_feature_ro_compat);
	printf("uuid = ");
	for(int i = 0; i <16; i++)
		printf("%d",super.s_uuid[i]);
	printf("\n");
	printf("volumn_name: %s\n", super.s_volumn_name);
	printf("last mounted dir: %s\n", super.s_last_mounted);
	disp(super.s_algorithm_usage_bitmap);

	close(fd);
}

int main(int argc, char** argv){
	if( argc != 2){
		fprintf(stderr, "Usage: display_super_block device\n");
		exit(1);
	}
	display_super_block(argv[1]);
	return 0;
}
